package com.sprometheus.util;

import cn.hutool.core.util.EnumUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import org.apache.commons.lang3.EnumUtils;
import org.aspectj.lang.JoinPoint;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 风白羽
 * @date 2024/9/5 下午4:55
 */
public class GenerateUtil {

    /***
     * feign的uri
     * @param targetClass
     * @param url
     * @return
     */
    public static String getApiRequestUri(Class<?> targetClass, Method method, String url) {
        FeignClient feignClient = targetClass.getAnnotation(FeignClient.class);
        if (feignClient != null) {
            StringBuilder builder = new StringBuilder("/");
            builder.append(feignClient.name());
            builder.append("/");
            builder.append(feignClient.path());
            PostMapping postMapping = method.getAnnotation(PostMapping.class);
            GetMapping getMapping = method.getAnnotation(GetMapping.class);
            RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
            if (postMapping == null && getMapping == null && requestMapping == null) {
                return url;
            }
            String[] value = new String[]{};
            if (postMapping != null) {
                value = postMapping.value();
            }
            if (getMapping != null) {
                value = getMapping.value();
            }
            if (requestMapping != null) {
                value = requestMapping.value();
            }
            if (value.length == 0) {
                return url;
            }
            builder.append(value[0].startsWith("/") ? value[0] : "/" + value[0]);
            builder.append(",feign[");
            builder.append(targetClass.getSimpleName());
            builder.append("]");
            url = builder.toString().replaceAll("//", "/");
        }
        return url;
    }

    public static String getParameter(JoinPoint point) {
        List<String> params = new ArrayList<>();
        Object[] args = point.getArgs();
        for (Object obj : args) {
            if (obj instanceof HttpServletResponse) {
                continue;
            }
            if (obj instanceof HttpServletRequest) {
                continue;
            }
            if (obj instanceof MultipartFile) {
                continue;
            }
            if (obj instanceof BindingResult) {
                continue;
            }
            params.add(JSONUtil.toJsonStr(obj));
        }
        return params.toString();
    }

    /***
     * 获取nginx 代理ip x-forwarded-for
     * @param request
     * @return
     */
    public static String getForwardedIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (StrUtil.isNotBlank(ip)) {
            String[] values = ip.split(",");
            if (values.length > 0) {
                ip = values[0];
            }
        }
        return StrUtil.isBlank(ip) ? request.getRemoteAddr() : ip;
    }
}
